
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets;

import entities.Utilisateur;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import session.GestionnaireUtilisateurs;

/**
 *
 * @author asus
 */
@WebServlet(name = "ServletAuthentification", urlPatterns = {"/ServletAuthentification"})
public class ServletAuthentification extends HttpServlet {

    @EJB
    private GestionnaireUtilisateurs gestionnaireUtilisateurs;
    private Utilisateur u;
    private Utilisateur v;
    private Collection<Utilisateur> liste = new ArrayList<Utilisateur>();

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        HttpSession session = request.getSession(true);
        PrintWriter out = response.getWriter();
        String action = request.getParameter("action");
        String login = request.getParameter("login");
        String mail = request.getParameter("mail");
        String password = request.getParameter("password");
        String passwordBis = request.getParameter("passwordBis");
        String adresse = request.getParameter("adresse");
        String nom = request.getParameter("nom");
        String prenom = request.getParameter("prenom");
        String statut = request.getParameter("statut");
        String message;
        String forwardTo = "";
        String message2 = "";


        try {
            if (action != null) {

                if (action.equals("authentifier")) {

                    request.setAttribute("listeDesUsers", liste);
                    RequestDispatcher dp2 = request.getRequestDispatcher("logIn.jsp?action=authentifier");
                    dp2.forward(request, response);
                } else if (action.equals("authentifierLog")) {
                    Collection<Utilisateur> liste = gestionnaireUtilisateurs.getAllUsers();
                    Iterator iterator = liste.iterator();
                    while (iterator.hasNext()) {
                        Utilisateur utilisateur = (Utilisateur) iterator.next();
                        String log = utilisateur.getMail();
                        String mdp = utilisateur.getPassword();


                        if ((log.equals(mail)) && (mdp.equals(password)) && (mail.equals("admin@admin.com")) && (password.equals("admin"))) {
                           Utilisateur u = gestionnaireUtilisateurs.getUserByMail(mail);
                            System.out.println("connection réussie");
                            session.setAttribute("connecte", true);
                            session.setAttribute("mail", mail);
                            session.setAttribute("u",u);
                            RequestDispatcher dp2 = request.getRequestDispatcher("admin.jsp?action=admin");
                            dp2.forward(request, response);


                        } else if ((log.equals(mail)) && (mdp.equals(password))) {
                            Utilisateur u = gestionnaireUtilisateurs.getUserByMail(mail);
                            System.out.println("connection réussie");
                            message = "Bonjour " + u.getLastname() + " " + u.getFirstname();
                            session.setAttribute("connecte", true);
                            session.setAttribute("mail", mail);
                            session.setAttribute("message", message);
                            String adresseU = u.getAdresse();
                            session.setAttribute("adresseU",adresseU);
                            String nomU = u.getFirstname();
                            session.setAttribute("nomU",nomU);                             
                            String prenomU = u.getLastname();
                            session.setAttribute("prenomU",prenomU);
                            String pass = u.getPassword();
                            session.setAttribute("pass",pass);
                            
                            
                            out.println(message);
                            request.getRequestDispatcher("index.jsp").forward(request, response);
                        }
                    }
                } else if (action.equals("logPanier")) {
                    Collection<Utilisateur> liste = gestionnaireUtilisateurs.getAllUsers();
                    Iterator iterator = liste.iterator();
                    while (iterator.hasNext()) {
                        Utilisateur utilisateur = (Utilisateur) iterator.next();
                        String log = utilisateur.getMail();
                        String mdp = utilisateur.getPassword();

                        if ((log.equals(mail)) && (mdp.equals(password))) {
                            Utilisateur u = gestionnaireUtilisateurs.getUserByMail(mail);
                            System.out.println("connection réussie");
                            message = "Bonjour " + u.getLastname() + " " + u.getFirstname();
                            session.setAttribute("connecte", true);
                            session.setAttribute("mail", mail);
                            session.setAttribute("message", message);
                            out.println(message);
                            request.getRequestDispatcher("panier.jsp").forward(request, response);
                        }
                    }
                }
                if (action.equals("inscription")) {
                    Utilisateur u = gestionnaireUtilisateurs.getUserByMail(mail);
                    if (u == null) {
                        u = gestionnaireUtilisateurs.creeUtilisateur(nom, prenom, adresse, mail, password, passwordBis);
                        liste.add(u);
                        message = "Bonjour " + u.getLastname() + " " + u.getFirstname();
                        session.setAttribute("connecte", true);
                        session.setAttribute("mail", mail);
                        session.setAttribute("nom", nom);
                        session.setAttribute("prenom", prenom);
                        session.setAttribute("message", message);
                        System.out.println(message);
                        System.out.println("utilisateur " + mail + " créé ! ");
                        request.getRequestDispatcher("index.jsp").forward(request, response);
                    } else {
                        System.out.append("L'utilisateur " + mail + "existe déjà ! ");
//                        forwardTo = "registration.jsp";
//                        message2 = "L'utilisateur existe déjà";
                        request.getRequestDispatcher("registration.jsp").forward(request, response);
                    }

                } else if (action.equals("index")) {
                    request.getRequestDispatcher("index.jsp").forward(request, response);
                } else if (action.equals("all")) {
                    request.getRequestDispatcher("all.jsp").forward(request, response);
                } else if (action.equals("browse")) {
                    request.getRequestDispatcher("browse.jsp").forward(request, response);
                } else if (action.equals("deconnection")) {
                    System.out.println("deconnection");
                    session.setAttribute("connecte", false);
                    request.getRequestDispatcher("index.jsp").forward(request, response);


                } else if (action.equals("account")) {
                    request.getRequestDispatcher("account.jsp").forward(request, response);
                } else if (action.equals("admin")) {
                    Collection<Utilisateur> listeUser = gestionnaireUtilisateurs.getAllUsers();
                    session.setAttribute("connecte", true);
                    session.setAttribute("mail", mail);
                    request.setAttribute("authentification", listeUser);

                    RequestDispatcher dp = request.getRequestDispatcher("admin.jsp");
                    dp.forward(request, response);
                } else if (action.equals("register")) {
                    request.getRequestDispatcher("registration.jsp").forward(request, response);
                }
                 else if (action.equals("info")) {
                   
                    request.getRequestDispatcher("info.jsp").forward(request, response);
                }
                 else if(action.equals("update")){
                    Utilisateur u = gestionnaireUtilisateurs.getUserByMail(mail);                 
                    u.setFirstname(request.getParameter("prenom"));
                    u.setLastname(request.getParameter("nom"));
                    u.setAdresse(request.getParameter("adresse"));
                    u.setPassword(request.getParameter("password"));
                    gestionnaireUtilisateurs.update(u);
                 }
            } else if (login.equals("admin@admin.com")) {
                RequestDispatcher dp = request.getRequestDispatcher("admin.jsp");
                dp.forward(request, response);
            } 

        } finally {
            out.close();
        }

//        RequestDispatcher dp = request.getRequestDispatcher(forwardTo + "&message=" + message2);
//        dp.forward(request, response);
    } // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
